#include <crt_log.h>
#include <crt_memory.h>
#include <crt_tstring.h>
#include <crt_stdtime.h>

#define LOG_FORMAT       	"[%s][%s][%s][%ld]==>"
#define LOG_PAGE_SIZE		4096

#ifdef __ANDROID__
/*
 * Send a simple string to the log.
 * int __android_log_write(int prio, const char* tag, const char* text);
 */
#include <jni.h>
#include <android/log.h>

#endif

static int _log_print(int proi,
	const char *tag,
	const char *file,
	long line,
	const char* func,
	const char *fmt,
	va_list ap)
{


	char* msg = NULL;
	int hmsglen = 0;
	int msglen = 0;
	char tm[81] = { 0x00 };
	int rc;
	rc = get_time_t(tm, 80);
	rc_assert(rc != S_ERROR, E_FAIL)
	
	msg = (char*)heap_calloc(LOG_PAGE_SIZE, sizeof(char));
	rc_assert(msg != NULL, S_ERROR)
	hmsglen = snprintf(msg, LOG_PAGE_SIZE, LOG_FORMAT, tag, tm, func, line);

	 if(hmsglen > 0 && hmsglen < LOG_PAGE_SIZE){
		 msglen = LOG_PAGE_SIZE - hmsglen;
	 } else {
		 msglen = LOG_PAGE_SIZE;
		 hmsglen = 0;
	 }
	 vsnprintf(msg + hmsglen, msglen, fmt, ap);

#ifdef __ANDROID__
	 __android_log_write(proi, "", msg);
#else
	debug_view(msg);
	debug_view("\n");
#endif

	heap_free(msg);
    return S_SUCCESS;
}


int log_print(int proi,
	const char *tag,
	const char *data,
	const char *time,
	const char *file,
	long line,
	const char* func,
	const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);

	_log_print(proi, tag, file, line, func, fmt, ap);
	
	va_end(ap);
	return S_SUCCESS;
}



